<?php
require_once '../model/mTrabAreas.php';

class aTrabAreas  extends mTrabAreas{
    
    protected $slqInsert ="SELECT INTO  trab_areas (cod_trabalho,cod_area 
        VALUES ('%s','%s')";
    
    // não tem update
    
    protected $sqlDelete ="DELETE FROM trab_areas WHERE cod_trabalho ='%s' 
        AND cod_area='%s'";
    
    protected $sqlSelect ="SELECT * FROM trab_areas WHERE 1=1 %s %s";
    
    // seleção com inner join
    
    protected $selectInner ="SELECT trabalhos.titulo, areas.nome_area, trab_areas . * 
FROM trab_areas
INNER JOIN trabalhos ON ( trabalhos.cod_trabalho = trab_areas.cod_trabalho ) 
INNER JOIN areas ON ( trab_areas.cod_area = areas.cod_area WHERE 1=1 %s %s ) ";
    
    
    public function insert(){
        $sql =sprintf($this->slqInsert.$this->getCodTrabalho(),
                      $this->slqInsert.$this->getCodArea());
        return $this->RunQuery($sql);
    }
    public function delete(){
        $sql=sprintf($this->sqlDelete.$this->getCodTrabalho(),
                     $this->sqlDelete.$this->getCodArea());
        return $this->RunQuery($sql);
    }
    
    public function select($where='',$order=''){
        $sql=sprintf($this->sqlSelect.$where.$order);
        return $this->RunSelect($sql);
        } 
    public function selectInner($where='',$order=''){
        $sql= sprintf($this->selectInner.$where, $order);
        return $this->RunSelect($sql);
    }
    
    public function load(){
        $rs =$this->select(sprintf("AND cod_trabalho='%s' AND cod_area='%s'",
                $this->getCodTrabalho(),
                $this->getCodArea()
                ));
        $this->setCodTrabalho($rs[0]['cod_trabalho']);
        $this->setCodArea($rs[0]['cod_area']);
        return $this;    
    }
}